﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.IO;
using PokeData;

namespace PokeTD_MapEditor
{
    public partial class TowerEdit : Form
    {
        public TowerEdit(WavesDS myDS)
        {
            InitializeComponent();
            wds = myDS;
        }

        WavesDS wds;
        Dictionary<int, int> UpgradeDict = new Dictionary<int, int>();

        private string _filename;

        public string FileName
        {
            get { return _filename; }
            set { _filename = value; }
        }

        #region number only
        private void nurZahlen(object sender, KeyEventArgs e)
        {
            //Allow navigation keyboard arrows
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                case Keys.PageUp:
                case Keys.PageDown:
                    e.SuppressKeyPress = false;
                    return;
                default:
                    break;
            }

            //Block non-number characters
            char currentKey = (char)e.KeyCode;
            bool modifier = e.Control || e.Alt || e.Shift;
            bool nonNumber = char.IsLetter(currentKey) ||
                             char.IsSymbol(currentKey) ||
                             char.IsWhiteSpace(currentKey) ||
                             char.IsPunctuation(currentKey);

            if (!modifier && nonNumber)
                e.SuppressKeyPress = true;

            //Handle pasted Text
            if (e.Control && e.KeyCode == Keys.V)
            {
                //Preview paste data (removing non-number characters)
                string pasteText = Clipboard.GetText();
                string strippedText = "";
                for (int i = 0; i < pasteText.Length; i++)
                {
                    if (char.IsDigit(pasteText[i]))
                        strippedText += pasteText[i].ToString();
                }

                if (strippedText != pasteText)
                {
                    //There were non-numbers in the pasted text
                    //e.SuppressKeyPress = true;

                    //OPTIONAL: Manually insert text stripped of non-numbers
                    TextBox me = (TextBox)sender;
                    int start = me.SelectionStart;
                    string newTxt = me.Text;
                    newTxt = newTxt.Remove(me.SelectionStart, me.SelectionLength); //remove highlighted text
                    newTxt = newTxt.Insert(me.SelectionStart, strippedText); //paste
                    me.Text = newTxt;
                    me.SelectionStart = start + strippedText.Length;
                }
                else
                    e.SuppressKeyPress = false;
            }
        }
        private void nurDezimalzahlen(object sender, KeyEventArgs e)
        {
            //Allow navigation keyboard arrows
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                case Keys.PageUp:
                case Keys.PageDown:
                case Keys.Decimal:
                    e.SuppressKeyPress = false;
                    return;
                default:
                    break;
            }

            //Block non-number characters
            char currentKey = (char)e.KeyCode;
            bool modifier = e.Control || e.Alt || e.Shift;
            bool nonNumber = char.IsLetter(currentKey) ||
                             char.IsSymbol(currentKey) ||
                             char.IsWhiteSpace(currentKey) ||
                             char.IsPunctuation(currentKey);

            if (!modifier && nonNumber)
                e.SuppressKeyPress = true;

            //Handle pasted Text
            if (e.Control && e.KeyCode == Keys.V)
            {
                //Preview paste data (removing non-number characters)
                string pasteText = Clipboard.GetText();
                string strippedText = "";
                for (int i = 0; i < pasteText.Length; i++)
                {
                    if (char.IsDigit(pasteText[i]))
                        strippedText += pasteText[i].ToString();
                }

                if (strippedText != pasteText)
                {
                    //There were non-numbers in the pasted text
                    //e.SuppressKeyPress = true;

                    //OPTIONAL: Manually insert text stripped of non-numbers
                    TextBox me = (TextBox)sender;
                    int start = me.SelectionStart;
                    string newTxt = me.Text;
                    newTxt = newTxt.Remove(me.SelectionStart, me.SelectionLength); //remove highlighted text
                    newTxt = newTxt.Insert(me.SelectionStart, strippedText); //paste
                    me.Text = newTxt;
                    me.SelectionStart = start + strippedText.Length;
                }
                else
                    e.SuppressKeyPress = false;
            }
        }
        #endregion

        private void TowerEdit_Load(object sender, EventArgs e)
        {
            cbTyp1.SelectedIndex = 0;
            cbTyp2.SelectedIndex = 0;
            cbUpgrade.Items.Add(new ComboboxItem("Kein", -1));
            UpgradeDict.Add(-1, 0);
            cbUpgrade.SelectedIndex = 0;
            cbEffekt.SelectedIndex = 0;

            if (wds.Towers.Rows.Count == 0)
            {
                splitContainer1.Enabled = false;
            }

            var res = from a in wds.Towers
                    select a;
            foreach (WavesDS.TowersRow r in res)
            {
                lbTower.Items.Add(new ComboboxItem(r.Name, r.id));
                cbUpgrade.Items.Add(new ComboboxItem(r.Name, r.id));
                UpgradeDict.Add(r.id, cbUpgrade.Items.Count - 1); 
            }
        }

        private void lbTower_SelectedIndexChanged(object sender, EventArgs e)
        {
            var res = from a in wds.Towers
                      where a.id == (int)(lbTower.SelectedItem as ComboboxItem).Value
                      select a;
            WavesDS.TowersRow r = res.First<WavesDS.TowersRow>();

            txtName.Text = r.Name;
            txtPreis.Text = r.Cost.ToString();
            cbTyp1.SelectedIndex = r.Type1 + 1;
            cbTyp2.SelectedIndex = r.Type2 + 1;
            cbUpgrade.SelectedIndex = UpgradeDict[r.UpgradeTo];
            cbIsBase.Checked = r.isbase;
            txtAttMin.Text = r.AttackMin.ToString();
            txtAttMax.Text = r.AttackMax.ToString();
            txtSpeed.Text = r.AttackSpeed.ToString();
            cbSpAttack.Checked = r.SpAttack;
            cbCanAttackAir.Checked = r.CanAttackAir;
            cbCanAttackGround.Checked = r.CanAttackGround;
            txtRange.Text = r.Range.ToString();
            cbEffekt.SelectedIndex = cbEffekt.FindString(r.EffectName);
            txtProbability.Text = r.EffectProbability.ToString();
            txtDuration.Text = r.EffectDuration.ToString();
        }

        private void SaveToDatabase(object sender, EventArgs e)
        {
            var res = from a in wds.Towers
                      where a.id == (int)(lbTower.SelectedItem as ComboboxItem).Value
                      select a;
            WavesDS.TowersRow r = res.First<WavesDS.TowersRow>();

            r.Name = txtName.Text;
            r.Cost = int.Parse(txtPreis.Text);
            r.Type1 = cbTyp1.SelectedIndex - 1;
            r.Type2 = cbTyp2.SelectedIndex - 1;
            r.UpgradeTo = UpgradeDict.FindKeyByValue<int, int>(cbUpgrade.SelectedIndex);
            r.isbase = cbIsBase.Checked;
            r.AttackMin = int.Parse(txtAttMin.Text);
            r.AttackMax = int.Parse(txtAttMax.Text);
            r.AttackSpeed = int.Parse(txtSpeed.Text);
            r.SpAttack = cbSpAttack.Checked;
            r.CanAttackAir = cbCanAttackAir.Checked;
            r.CanAttackGround = cbCanAttackGround.Checked;
            r.Range = double.Parse(txtRange.Text);
            try
            {
                r.EffectName = cbEffekt.Items[cbEffekt.SelectedIndex].ToString();
            }
            catch (Exception ex)
            {
                r.EffectName = "";
            }
            r.EffectProbability = int.Parse(txtProbability.Text);
            r.EffectDuration = decimal.Parse(txtDuration.Text);

            UpgradeDict.Clear();
            lbTower.Items.Clear();
            cbUpgrade.Items.Clear();
            cbUpgrade.Items.Add(new ComboboxItem("Kein", -1));
            UpgradeDict.Add(-1, 0);
            var res2 = from a in wds.Towers
                      select a;
            foreach (WavesDS.TowersRow rw in res2)
            {
                lbTower.Items.Add(new ComboboxItem(rw.Name, rw.id));
                cbUpgrade.Items.Add(new ComboboxItem(rw.Name, rw.id));
                UpgradeDict.Add(rw.id, cbUpgrade.Items.Count - 1);
            }
            lbTower.SelectedIndex = lbTower.FindString(r.Name);
        }

        private void bttnNeu_Click(object sender, EventArgs e)
        {
            wds.Towers.AddTowersRow("Neuer Tower", 0, 0, 0, false, 0, true, 0, -1, 0, 0, true, true, "", 0, 0);
            UpgradeDict.Clear();
            lbTower.Items.Clear();
            cbUpgrade.Items.Clear();
            cbUpgrade.Items.Add(new ComboboxItem("Kein", -1));
            UpgradeDict.Add(-1, 0);
            var res2 = from a in wds.Towers
                       select a;
            foreach (WavesDS.TowersRow rw in res2)
            {
                lbTower.Items.Add(new ComboboxItem(rw.Name, rw.id));
                cbUpgrade.Items.Add(new ComboboxItem(rw.Name, rw.id));
                UpgradeDict.Add(rw.id, cbUpgrade.Items.Count - 1);
            }
            lbTower.SelectedIndex = lbTower.FindString("Neuer Tower");
            splitContainer1.Enabled = true;
        }

        private void bttnLoeschen_Click(object sender, EventArgs e)
        {
            var res = from a in wds.Towers
                      where a.id == (int)(lbTower.SelectedItem as ComboboxItem).Value
                      select a;
            WavesDS.TowersRow r = res.First<WavesDS.TowersRow>();

            r.Delete();

            UpgradeDict.Clear();
            lbTower.Items.Clear();
            cbUpgrade.Items.Clear();
            cbUpgrade.Items.Add(new ComboboxItem("Kein", -1));
            UpgradeDict.Add(-1, 0);
            var res2 = from a in wds.Towers
                       select a;
            foreach (WavesDS.TowersRow rw in res2)
            {
                lbTower.Items.Add(new ComboboxItem(rw.Name, rw.id));
                cbUpgrade.Items.Add(new ComboboxItem(rw.Name, rw.id));
                UpgradeDict.Add(rw.id, cbUpgrade.Items.Count - 1);
            }
            lbTower.SelectedIndex = 0;
        }
    }
}
